package com.bcsq.todolistdemo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.bcsq.todolistdemo.camera.CameraActivity;
import com.bcsq.todolistdemo.demo.AddGroupActivity;
import com.bcsq.todolistdemo.demo.activity.ToDoListActivity;
import com.bcsq.todolistdemo.demo.adapter.GroupAdapter;
import com.bcsq.todolistdemo.video.VideoActivity;
import com.bcsq.todolistdemo.video.VideoUploadActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Toolbar toolbar;
    Context mContext;
    static int RESULT_CODE = 1000;

    //打开相机按钮
    Button btnOpenCamera;
    Button btnOpenVideo;
    Button btnVideoUpload;

    ListView lvGroups;
    List<Map.Entry<String, Integer>> groups;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        findByView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences("groups", MODE_PRIVATE);
        Map<String, Integer> all = (Map<String, Integer>) sharedPreferences.getAll();
        Set set = all.entrySet();
        groups = new ArrayList<>(set);

        lvGroups.setAdapter(new GroupAdapter(mContext, groups));
    }

    private void findByView() {
        mContext = this;
        btnOpenCamera = findViewById(R.id.btn_openCamera);
        btnOpenVideo = findViewById(R.id.btn_open_video);
        lvGroups = findViewById(R.id.lv_groups);
        toolbar = findViewById(R.id.toolbar);
        btnVideoUpload = findViewById(R.id.btn_video_upload);

        btnVideoUpload.setOnClickListener(this);
        btnOpenCamera.setOnClickListener(this);
        btnOpenVideo.setOnClickListener(this);

        toolbar.setPadding(10, 0, 10, 0);
        toolbar.setTitle("toolbar分组列表");
        toolbar.setSubtitle("subTitle");
        toolbar.setLogo(R.mipmap.ic_launcher);
        //替换默认的toolbar为自己的toolbar
        setSupportActionBar(toolbar);
        //给main页面添加列表
        lvGroups.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String groupName = groups.get(i).getKey();
                Intent intent = new Intent(mContext, ToDoListActivity.class);
                intent.putExtra("groupName", groupName);
                startActivity(intent);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //菜单填充器
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_add, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.item_add :
                Intent intent = new Intent(mContext, AddGroupActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_openCamera:
                Intent intent1 = new Intent(mContext, CameraActivity.class);
                startActivity(intent1);
                break;
            case R.id.btn_open_video :
                Intent intent = new Intent(mContext, VideoActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_video_upload :
                Intent intent2 = new Intent(mContext, VideoUploadActivity.class);
                startActivity(intent2);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        
//        if (requestCode == requestCode && resultCode == resultCode){
//            String imgData = data.getStringExtra("imgData");
//        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
